<!--
 * @Author: Lzx
 * @LastEditors: Lzx
 * @Date: 2021-06-24 09:06:36
 * @LastEditTime: 2022-04-22 22:47:48
 * @FilePath: /web-vue/src/App.vue
 * @Description:
-->
<template>
  <div id="app">
    <!-- 通过url在router里匹配组件 -->
    <transition name="el-fade-in-linear">
      <router-view></router-view>
    </transition>
    <!-- TODO-lzx 不知为何无效：回到顶部 -->
    <el-backtop :bottom="100" :visibility-height="50"></el-backtop>
  </div>
</template>

<script>
// function handleScreen() {
//   const m = detectZoom();
//   document.body.style.zoom = 100 / Number(m);
// }
// function detectZoom() {
//   let ratio = 0,
//     screen = window.screen,
//     ua = navigator.userAgent.toLowerCase();
//   if (window.devicePixelRatio !== undefined) {
//     ratio = window.devicePixelRatio;
//   } else if (~ua.indexOf("msie")) {
//     if (screen.deviceXDPI && screen.logicalXDPI) {
//       ratio = screen.deviceXDPI / screen.logicalXDPI;
//     }
//   } else if (
//     window.outerWidth !== undefined &&
//     window.innerWidth !== undefined
//   ) {
//     ratio = window.outerWidth / window.innerWidth;
//   }
//   if (ratio) {
//     ratio = Math.round(ratio * 100);
//   }
//   return ratio;
// }
// window.onresize = function () {
//   handleScreen();
// };

// window.onload = function () {
//   handleScreen();
// };

export default {
  mounted() {
    // 刷新页面的时候重新加载字典列表，不需要提示未认证
    this.initCommonDict();
    // 初始化部分页面的权限控制
    this.initAuth();
  },

  methods: {
    // 初始化通用字典信息
    initCommonDict() {
      // 1）获取部门列表
      this.$api
        .querySysDepartment()
        .then((res) => {
          let { status, message, data } = res.data; // 响应数据
          if (status == 200) {
            this.$store.state.DICT.deptList = data;
          }
        })
        .catch(function (err) {
          loading.close();
          vueIns.visible = false;
          console.log(`【部门列表】查询接口发生错误：${err}`);
        });

      // 2）获取系统用户列表
      this.$api
        .querySysUser({ pageSize: 1000 })
        .then((res) => {
          let { status, message, data } = res.data; // 响应数据
          if (status == 200) {
            this.$store.state.DICT.sysUserList = data.items;
          }
        })
        .catch(function (err) {
          loading.close();
          vueIns.visible = false;
          console.log(`【系统用户列表】查询接口发生错误：${err}`);
        });

      // 5）获取系统角色列表
      this.$api
        .querySysRoleList()
        .then((res) => {
          let { status, message, data } = res.data; // 响应数据
          if (status == 200) {
            this.$store.state.DICT.sysRoleList = data;
          }
        })
        .catch(function (err) {
          loading.close();
          vueIns.visible = false;
          console.log(`【系统角色列表】查询接口发生错误：${err}`);
        });

      // 6）获取房源类型列表
      this.$api
        .queryHouseTypeDictList({ pageSize: 1000 })
        .then((res) => {
          let { status, message, data } = res.data; // 响应数据
          if (status == 200) {
            this.$store.state.DICT.houseTypeSelect = data;
          }
        })
        .catch(function (err) {
          loading.close();
          vueIns.visible = false;
          console.log(`【房源类型列表】查询接口发生错误：${err}`);
        });

      // 7）获取地区列表
      this.$api
        .queryRegionTreeList()
        .then((res) => {
          let { status, message, data } = res.data; // 响应数据
          if (status == 200) {
            this.$store.state.DICT.regionCode = data;
          }
        })
        .catch(function (err) {
          loading.close();
          vueIns.visible = false;
          console.log(`【地区列表】查询接口发生错误：${err}`);
        });

      // 7）获取房源列表
      this.$api
        .queryHouseDictList()
        .then((res) => {
          let { status, message, data } = res.data; // 响应数据
          if (status == 200) {
            // list 转 map
            let map = new Map();
            data.forEach((element) => {
              map.set(element.code, element.name);
            });
            this.$store.state.DICT.houseMap = map;
          }
        })
        .catch(function (err) {
          loading.close();
          vueIns.visible = false;
          console.log(`【房源列表】查询接口发生错误：${err}`);
        });
    },

    // 初始化权限
    initAuth() {
      if (this.$store.state.userSession) {
        if (this.$store.state.userSession.roleIds.indexOf(1) > -1) {
          this.$store.state.auth.isDelete = true;
          this.$store.state.auth.isAdd = true;
          this.$store.state.auth.isEdit = true;
          this.$store.state.auth.isAppraise = true;
          this.$store.state.auth.isAudit = true;
        } else if (this.$store.state.userSession.roleIds.indexOf(5) > -1) {
          this.$store.state.auth.isEdit = true;
        } else if (
          this.$store.state.userSession.roleIds.indexOf(2) > -1 ||
          this.$store.state.userSession.roleIds.indexOf(7) > -1
        ) {
          this.$store.state.auth.isAdd = true;
          this.$store.state.auth.isAppraise = true;
        }
      }
    },
  },
};
</script>

<style lang="scss">
// 通用Layout布局样式
@import "@/assets/css/panel-layout.scss";

// 覆盖顶部加载条背景色
#nprogress .bar {
  background-color: #69eeff;
}
</style>
